package com.cpi.cornpp.user.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.user.exception.UserException;
import com.cpi.cornpp.user.form.UserForm;
import com.cpi.cornpp.user.services.UserServices;
import com.cpi.cornpp.user.view.UserView;

/**
 * UserAction
 * 
 * @author Edgar Joao
 *
 */
public class UserLogInAction extends Action {
	/** Nombre de variable de session para el usuario */
	private static final String USER_SESSION_NAME = "user";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS = "success";//$NON-NLS-1$
	/** Fail forward */
	private final static String FAILS = "fail";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws UserException {
		UserServices userServices = null;
		UserForm userForm = null;
		UsuarioApp usuario = null;
		UserView userView = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = null;
		HttpSession httpSession = null;
		
		try{
			httpSession = request.getSession(false);
			userServices = new UserServices();
			userForm = (UserForm) form;
			if(userForm == null){
				form = new UserForm();
			}
			messageResources = getResources(request);
			userView = userForm.getUserView();
			usuario = userServices.loginUser(userView);
			userView.setUserObject(usuario);
			request.getSession(false).setAttribute(USER_SESSION_NAME, userView);
			forward = SUCCESS;
		}catch(UserException userException){
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), userException.getErrorCode());
				if(null == messageError){
					httpSession.setAttribute(CUSTOM_EXCEPTION, userException);
					form.reset(mapping, request);
					throw userException;
				}else{
					httpSession.setAttribute(ERROR_MESSAGE, messageError);
					forward = FAILS;
				}
		}catch(Exception exception){
			UserException userException = null;
			exception.printStackTrace(System.out);
			userException = new UserException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LOGIN);
			httpSession.setAttribute(CUSTOM_EXCEPTION, userException);
			form.reset(mapping, request);
			throw userException;
		}
		form.reset(mapping, request);
		return mapping.findForward(forward);
	}
}